<?php

/**
 * physicalqty actions.
 *
 * @package    student_manager
 * @subpackage physicalqty
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2288 2006-10-02 15:22:13Z fabien $
 */
class physicalqtyActions extends autophysicalqtyActions
{
    public function executeEdit()
  {
     $this->getResponse()->setTitle('WMS-Edit-Physical');
    $this->physical_qty = $this->getPhysicalQtyOrCreate();

    if ($this->getRequest()->isMethod('post'))
    {
      $this->updatePhysicalQtyFromRequest();
                $this->physical_qty->setPsno(CommonValues::GenerateAutoNumber('PS', $this->physical_qty->getPsno(), 'PS')) ;

	      //Tien code here
	      $qtys = $this->getRequestParameter('qtys');
	      $rowIds = $this->getRequestParameter('rowIds');
	      

	      if (isset($rowIds) && count($rowIds) && isset($qtys) && count($qtys) ) {

	      	//remove old component group id
	      	if ($this->physical_qty->getComponentgroupId()) {
	      	    $c = new Criteria();
	      	    $c->add(ComponentsPeer::COMPONENTGROUP_ID , $this->physical_qty->getComponentgroupId());
	      	    ComponentsPeer::doDelete($c);
	      	}

	      	$componentGroup = new ComponentGroup();
	      	if ($componentGroup)    {
	      		$componentGroup->setType(1);
	      		$componentGroup->save();
	      		$componentGroupId = $componentGroup->getId();

	      		if ($componentGroupId) {
	      			$this->physical_qty->setComponentgroupId($componentGroupId);
	      			for($i=0; $i < count($rowIds); $i++){
	      				$components = new Components();
                                        $c = new Criteria();
                                        $c->add(WarehousePeer::COMPONENT_ID , $rowIds[$i]);
                                        $warehouse = WarehousePeer::doSelectOne($c);
	      				if ($components){
	      					$components->setComponentgroupId($componentGroupId);
	      					$components->setComponentId($rowIds[$i]);
	      					$components->setQty1($qtys[$i]);
	      					$components->setQty2($warehouse->getClosing());
	      					$components->save();
	      				}
	      			}
	      		}
	      	}
	      }

      try
      {
        $this->savePhysicalQty($this->physical_qty);
      }
      catch (PropelException $e)
      {
        $this->getRequest()->setError('edit', 'Could not save the edited Physical qtys.');
        return $this->forward('physicalqty', 'list');
      }

      $this->getUser()->setFlash('notice', 'Your modifications have been saved');

      if ($this->getRequestParameter('save_and_add'))
      {
        return $this->redirect('physicalqty/create');
      }
      else if ($this->getRequestParameter('save_and_list'))
      {
        return $this->redirect('physicalqty/list');
      }
      else
      {
        return $this->redirect('physicalqty/edit?id='.$this->physical_qty->getId());
      }
    }
    else
    {
      $this->labels = $this->getLabels();
    }
  }
  public function handleErrorEdit()
  {
    $this->preExecute();
    $this->physical_qty = $this->getPhysicalQtyOrCreate();
    $this->updatePhysicalQtyFromRequest();

    $this->labels = $this->getLabels();

    //transfer data to edit_form template
    $this->physical_qty->qtys = $this->getRequestParameter('qtys');
    $this->physical_qty->rowIds = $this->getRequestParameter('rowIds');

    return sfView::SUCCESS;
  }

  protected function getLabels()
  {
    return array(
      'physical_qty{id}' => 'Id:',
      'physical_qty{psno}' => 'Psno:',
      'physical_qty{componentgroup_id}' => 'Componentgroup:',
      'physical_qty{created_at}' => 'Created at:',
      'qtys'=>'Component List:',
    );
  }

  protected function addFiltersCriteria($c)
  {
    if (isset($this->filters['psno_is_empty']))
    {
      $criterion = $c->getNewCriterion(PhysicalQtyPeer::PSNO, '');
      $criterion->addOr($c->getNewCriterion(PhysicalQtyPeer::PSNO, null, Criteria::ISNULL));
      $c->add($criterion);
    }
    else if (isset($this->filters['psno']) && $this->filters['psno'] !== '')
    {
      $c->add(PhysicalQtyPeer::PSNO, '%'.strtr($this->filters['psno'], '*', '%').'%', Criteria::LIKE);
    }
    if (isset($this->filters['created_at_is_empty']))
    {
      $criterion = $c->getNewCriterion(PhysicalQtyPeer::CREATED_AT, '');
      $criterion->addOr($c->getNewCriterion(PhysicalQtyPeer::CREATED_AT, null, Criteria::ISNULL));
      $c->add($criterion);
    }
    else if (isset($this->filters['created_at']))
    {
      if (isset($this->filters['created_at']['from']) && $this->filters['created_at']['from'] !== '')
      {
        $criterion = $c->getNewCriterion(PhysicalQtyPeer::CREATED_AT, date('Y-m-d', $this->filters['created_at']['from']), Criteria::GREATER_EQUAL);
      }
      if (isset($this->filters['created_at']['to']) && $this->filters['created_at']['to'] !== '')
      {
        if (isset($criterion))
        {
          $criterion->addAnd($c->getNewCriterion(PhysicalQtyPeer::CREATED_AT, date('Y-m-d', $this->filters['created_at']['to']), Criteria::LESS_EQUAL));
        }
        else
        {
          $criterion = $c->getNewCriterion(PhysicalQtyPeer::CREATED_AT, date('Y-m-d', $this->filters['created_at']['to']), Criteria::LESS_EQUAL);
        }
      }

      if (isset($criterion))
      {
        $c->add($criterion);
      }
    }
  }
  public function executePublish() {
    $Id = $this->getRequestParameter('id');
    //$physical_qty = new TallyIn();
    $physical_qty = PhysicalQtyPeer::retrieveByPK($Id);
    if (!$physical_qty ){
        $this->getRequest()->setError('publish', 'This PS is not available.');
        $this->forward('physicalqty', 'list');
    }

    if ($physical_qty->getIspublish()) {
        $this->getRequest()->setError('publish', 'This PS has been published adready.');
        $this->forward('physicalqty','list');
    }

    $componentGroupId = $physical_qty->getComponentgroupId();
    $c = new Criteria();
    $c->add(ComponentsPeer::COMPONENTGROUP_ID , $componentGroupId);
    $componentsList = ComponentsPeer::doSelect($c);
    if (!$componentsList || !count($componentsList)){
        $this->getRequest()->setError('publish', 'This PS has not any components.');
        $this->forward('physicalqty','list');
    }

    foreach ($componentsList as $components) {
        $componentId = $components->getComponentId();
        $c = new Criteria();
        $c->add(WarehousePeer::COMPONENT_ID , $componentId);
        $warehouse = WarehousePeer::doSelectOne($c);
        
        if (!$warehouse){
            $this->getRequest()->setError('publish', "Component '". $components->getComponent(). "' is not available.");
            $this->forward('physicalqty','list');
        }
        $warehouse->setClosing($components->getQty1());
        $warehouse->save();
    }


    $physical_qty->setIspublish(1);
    $physical_qty->save();

    $this->getUser()->setFlash('notice', 'This PS was published successfully.');

    $this->redirect('physicalqty/list?id='.$Id);
}
}
